import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入
import Reg from '../views/Reg/Reg.vue'
import Login from '../views/Login/Login.vue'
import Main from '../views/Main/Main.vue'
import store from '../store/index'
import Home from '../views/Menus/Home/Home.vue'
import userinfo from '../views/Menus/User/UserInfo.vue'
import ArtCate from '../views/Menus/Article/ArtCate.vue'

Vue.use(VueRouter)
// 配置路由规则
const routes = [
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home
      }, {
        path: '/user-info',
        component: userinfo
      }, {
        path: 'art-cate',
        component: ArtCate
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 前置守卫
router.beforeEach((to, from, next) => {
  if (!store.state.token && to.path !== '/login' && to.path !== '/reg') {
    Vue.prototype.$message.error('您没有登录，请先登录')
    next('/login')
  }
  next()
})

export default router
